﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using WhereAmiMvc.Dbs;
using WhereAmiMvc.Entities;

namespace WhereAmiMvc.Controllers
{
    public class LocationController : Controller
    {
        LocationsBll bll = new LocationsBll();
        // GET: Location
        public ActionResult Index(int page=1)
        {
            var list = bll.GetAll().ToPagedList(page,10);

            return View(list);
        }

        // GET: Location/Details/5
        public ActionResult Details(string id)
        {
            var obj = bll.GetByKey(id);

            return View(obj);
        }

        // GET: Location/Create
        public ActionResult Create()
        {
            var obj = new Locations();

            obj.IdLoacations = Commons.GenerateId();

            return View(obj);
        }

        // POST: Location/Create
        [HttpPost]
        public ActionResult Create(Locations obj)
        {
            try
            {
                // TODO: Add insert logic here
                bll.Insert(obj);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Location/Edit/5
        public ActionResult Edit(string id)
        {
            var obj = bll.GetByKey(id);

            return View(obj);
        }

        // POST: Location/Edit/5
        [HttpPost]
        public ActionResult Edit(Locations obj)
        {
            try
            {
                // TODO: Add update logic here
                bll.Update(obj);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Location/Delete/5
        public ActionResult Delete(string id)
        {
            var obj = bll.GetByKey(id);

            return View(obj);
        }

        // POST: Location/Delete/5
        [HttpPost]
        public ActionResult Delete(string id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                bll.DeleteByKey(id);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        
    }
}
